function NewDrawPlayerStats()
{
WindowStyle.drawWindow(250, 333, 385, 142);

  Font.drawText(252, 335, Player.name + ": " + Player.hp + "/" + Player.maxhp + " Mp: " + Player.mp + "/" + Player.maxmp);
  
  if (Player2.pres == true)
  {
    Font.drawText(252, 355, Player2.name + ": " + Player2.hp + "/" + Player2.maxhp + " Mp: " + Player2.mp + "/" + Player2.maxmp + "  " + Player2.order + "  " + Player2.ordermax);
  }
  if (Player3.pres == true)
  {
    Font.drawText(252, 375, Player3.name + ": " + Player3.hp + "/" + Player3.maxhp + " Mp: " + Player3.mp + "/" + Player3.maxmp + "  " + Player3.order + "  " + Player3.ordermax);
  }
}
function TimeBar()
{
Player.temporderbar = Math.floor(Player.order/Player.orderbar);
Player2.temporderbar = Math.floor(Player2.order/Player2.orderbar);
Player3.temporderbar = Math.floor(Player3.order/Player3.orderbar);

  if (Player.order < 0)
  {
  PlayerBar = LoadImage("Bar20.png");
  }
  else
  {
  PlayerBar = LoadImage("Bar" + Player.temporderbar + ".png");
  }

  if (Player2.order < 0)
  {
  Player2Bar = LoadImage("Bar20.png");
  }
  else
  {
  Player2Bar = LoadImage("Bar" + Player2.temporderbar + ".png");
  }

  if (Player3.order < 0)
  {
  Player3Bar = LoadImage("Bar20.png");
  }
  else
  {
  Player3Bar = LoadImage("Bar" + Player3.temporderbar + ".png");
  }

  PlayerBar.blit(252, 345);
  if (Player2.pres == true)
  {
    Player2Bar.blit(252, 365);
  }
  if (Player3.pres == true)
  {
    Player3Bar.blit(252, 385);
  }
}

function FightStart()
{
Battle.playing = true;
Battle.wait = false;
Player.order = 0;
Player2.order = 0;
Player3.order = 0;
Monster1.order = 0;
Monster2.order = 0;
Monster3.order = 0;
Monster4.order = 0;

Player.ordermax = Math.floor((1/Player.agi)*50*60);
Player2.ordermax = Math.floor((1/Player2.agi)*50*60);
Player3.ordermax = Math.floor((1/Player3.agi)*50*60);

Monster1.ordermax = Math.floor((1/Monster1.agi)*50*60);
Monster2.ordermax = Math.floor((1/Monster2.agi)*50*60);
Monster3.ordermax = Math.floor((1/Monster3.agi)*50*60);
Monster4.ordermax = Math.floor((1/Monster4.agi)*50*60);

Player.orderbar = Player.ordermax/20;
Player2.orderbar = Player2.ordermax/20;
Player3.orderbar = Player3.ordermax/20;

OrderMachine();
}

function OrderMachine()
{
RenderMap();
NewDrawPlayerStats();
FlipScreen();

  if (Monster1.hp <= 0 || Monster1.stun > 0)
  {
  }
  else
  {
  Monster1.order = Monster1.order + 1;
  }

  if (Monster2.hp <= 0 || Monster2.stun > 0)
  {
  }
  else
  {
  Monster2.order = Monster2.order + 1;
  }

  if (Monster3.hp <= 0 || Monster3.stun > 0)
  {
  }
  else
  {
  Monster3.order = Monster3.order + 1;
  }

  if (Monster4.hp <= 0 || Monster4.stun > 0)
  {
  }
  else
  {
  Monster4.order = Monster4.order + 1;
  }

  if (Player.hp <= 0 || Player.stun > 0)
  {
  }
  else
  {
  Player.order = Player.order + 1;
  }

  if (Player2.hp <= 0 || Player2.stun > 0)
  {
  }
  else
  {
  Player2.order = Player2.order + 1;
  }

  if (Player3.hp <= 0 || Player3.stun > 0)
  {
  }
  else
  {
  Player3.order = Player3.order + 1;
  }

Player.order = Player.order + 1;
Player2.order = Player2.order + 1;
Player3.order = Player3.order + 1;

  if (Monster1.order == Monster1.ordermax)
  {
    if (Monster1.hp >0)
    {
    Monster1Turn();
    Monster1.order = -10;
    }
  }
  if (Monster2.order == Monster2.ordermax)
  {
    if (Monster2.hp >0)
    {
    Monster2Turn();
    Monster2.order = -10;
    }
  }
  if (Monster3.order == Monster3.ordermax)
  {
    if (Monster2.hp >0)
    {
    Monster3Turn();
    Monster3.order = -10;
    }
  }
  if (Monster4.order == Monster4.ordermax)
  {
    if (Monster4.hp >0)
    {
    Monster4Turn();
    Monster4.order = -10;
    }
  }
  if (Player.order == Player.ordermax)
  {
    if (Player.hp >0)
    {
    Player1Turn();
    Player.order = -10;
    }
  }
  if (Player2.order == Player2.ordermax)
  {
    if (Player2.hp >0)
    {
    Player2Turn();
    Player2.order = -10;
    }
  }
  if (Player3.order == Player3.ordermax)
  {
    if (Player3.hp >0)
    {
    Player3Turn();
    Player3.order = -10;
    }
  }

  BattleQueue();
}

function BattleQueue()
{
  if (Battle.wait == false)
  {
  SetDelayScript(1, "OrderMachine()");
  }
  else
  {
  SetDelayScript(1, "BattleQueue()");
  }
}

function Monster1Turn()
{
//Choice here
SetDelayScript(10, "Monster1T()");
}
function Monster2Turn()
{
//Choice here
SetDelayScript(10, "Monster2T()");
}
function Monster3Turn()
{
//Choice here
SetDelayScript(10, "Monster3T()");
}
function Monster4Turn()
{
//Choice here
SetDelayScript(10, "Monster4T()");
}

function Player1Turn()
{
//Choice here
SetDelayScript(10, "Player1T()");
}
function Player2Turn()
{
//Choice here
SetDelayScript(10, "Player2T()");
}
function Player3Turn()
{
//Choice here
SetDelayScript(10, "Player3T()");
}




function Monster1T()
{
Battle.wait = true;
Delay(200);
Talk_Window("Monster1 has it's turn", "blank.png");
Delay(200);
Battle.wait = false;
Monster1.order = 0;
}

function Monster2T()
{
Battle.wait = true;
Delay(200);
Talk_Window("Monster2 has it's turn", "blank.png");
Delay(200);
Battle.wait = false;
Monster2.order = 0;
}

function Monster3T()
{
Battle.wait = true;
Delay(200);
Talk_Window("Monster3 has it's turn", "blank.png");
Delay(200);
Battle.wait = false;
Monster3.order = 0;
}

function Monster4T()
{
Battle.wait = true;
Delay(200);
Talk_Window("Monster4 has it's turn", "blank.png");
Delay(200);
Battle.wait = false;
Monster4.order = 0;
}

function Player1T()
{
  

  RenderMap();
  NewDrawPlayerStats();
  TimeBar();
      
  var BattleMenu = new Menu();
  BattleMenu.font         = Font;
  BattleMenu.window_style = WindowStyle;
  BattleMenu.arrow        = Arrow;
  BattleMenu.addItem("Fight",  BattleMenu_Fight);
    if (Player.chrclass == "Soldier")
    {
    BattleMenu.addItem("Sword Tech", BattleSpecialSoldierTech);
    }
    else
    {
    BattleMenu.addItem("Special",  BattleMenu_SpecialP1);
    }
  BattleMenu.addItem("Item",  BattleMenu_Item);
  BattleMenu.addItem("Defend",  BattleMenu_Defend);
  BattleMenu.addItem("Run", BattleMenu_Run);
  BattleMenu.execute(5, 355, 235, 120);
  }
}

function Player2T()
{
Player.active = "Player2";

  BattleName(); 
  DrawPlayerStats();
        
  var BattleMenu = new Menu();
  BattleMenu.font         = Font;
  BattleMenu.window_style = WindowStyle;
  BattleMenu.arrow        = Arrow;
  BattleMenu.addItem("Fight",  BattleMenu_Fight);
       
    if (Player2.chrclass == "Soldier")
    {
    BattleMenu.addItem("Sword Tech", BattleSpecialSoldierTech);
    }
    else
    {
    BattleMenu.addItem("Special",  BattleMenu_SpecialP1);
    }
  BattleMenu.addItem("Item",  BattleMenu_Item);
  BattleMenu.addItem("Defend",  BattleMenu_Defend);
  BattleMenu.addItem("Run", BattleMenu_Run);
  BattleMenu.execute(5, 355, 235, 120);
}
function Player3T()
{
  BattleName();
  DrawPlayerStats();
        
  var BattleMenu = new Menu();
  BattleMenu.font         = Font;
  BattleMenu.window_style = WindowStyle;
  BattleMenu.arrow        = Arrow;
  BattleMenu.addItem("Fight",  BattleMenu_Fight);
        
    if (Player3.chrclass == "Soldier")
    {
    BattleMenu.addItem("Sword Tech", BattleSpecialSoldierTech);
    }
    else
    {
    BattleMenu.addItem("Special",  BattleMenu_SpecialP1);
    }
  BattleMenu.addItem("Item",  BattleMenu_Item);
  BattleMenu.addItem("Defend",  BattleMenu_Defend);
  BattleMenu.addItem("Run", BattleMenu_Run);
  BattleMenu.execute(5, 355, 235, 120);
}

function BattleMenu_Defend()
{
  if (Player.active == "Player1")
  {
  Player.moddef = true;
  Player.defence = Player.defence*2;
  BattleMenu_Itemused();
  }
  else if (Player.active == "Player2")
  {
  Player2.moddef = true;
  Player2.defence = Player2.defence*2;
  BattleMenu_Itemused();
  }
  else if (Player.active == "Player3")
  {
  Player3.moddef = true;
  Player3.defence = Player3.defence*2;
  BattleMenu_Itemused();
  }
}